﻿using EyFramework.Common;
using EyFramework.Library;
using EyFramework.Sys.Core;
using Ncov2019.Core.Service;
using Ncov2019.Entity;

namespace Ncov2019
{
    public partial class FrmNcovOrderEdit : FrmDevEditBase
    {
        private INcovRecordsService ncovRecordsService = Globals.GetService<INcovRecordsService>();

        public FrmNcovOrderEdit()
        {
            InitializeComponent();
        }

        public override void InitForm()
        {
           DataBinder.BindingLookupEditDataSource(luePattype, SysCache.GetCacheSysDict("ncov_pat_type"), "DICT_LABEL", "DICT_LABEL");
           base.InitForm();
        }

        public override void DisplayData()
        {
            if (!string.IsNullOrEmpty(this.ID))
            {
                NcovRecords ncovRecord = ncovRecordsService.QueryById(long.Parse(this.ID));
                teOrderDt.Text = ncovRecord.ORD_DT;
                luePattype.EditValue = ncovRecord.PAT_TYPE;
                meNote.Text = ncovRecord.NOTE;
            }
        }

        public override void ClearScreen()
        {
            this.ID = "";
            this.teOrderDt.Text = ncovRecordsService.GetCurrentDate("YYYY-MM-DD");
            this.luePattype.EditValue = null;
            this.meNote.Text = "";
        }

        public override bool CheckInput()
        {
            if (string.IsNullOrEmpty(teOrderDt.Text))
            {
                Globals.ShowError("开单日期不能为空！");
                return false;
            }
            if (luePattype.EditValue == null)
            {
                Globals.ShowError("开单类型不能为空！");
                return false;
            }

            return base.CheckInput();
        }

        public override bool SaveUpdated()
        {
            var ncovRecord = FillData();
            ncovRecord.RECORD_SEQ = long.Parse(this.ID);
            return ncovRecordsService.UpdateNcovRecord(ncovRecord);
        }

        public override bool SaveAddNew()
        {
            return ncovRecordsService.AddNcovRecord(FillData());
        }

        private NcovRecords FillData()
        {
            NcovRecords ncovRecord = new NcovRecords();
            ncovRecord.ORD_DT = teOrderDt.DateTime.ToString("yyyy-MM-dd");
            ncovRecord.PAT_TYPE = SysCache.GetDictValue(luePattype.EditValue.ToString(), "ncov_pat_type");
            ncovRecord.ORDER_USR = Globals.CurrentUser.UserId;
            ncovRecord.NOTE = meNote.Text.Trim();
            return ncovRecord;
        }
    }
}